<?php

class ShopjobController extends AppController {

	public $name = 'shopjob';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','add','edit','del');
	}
	
	public function index() {
        $this->_teamRights();
        $page = intval(Request::get_args('page'));		
		$conditions = array();	
		$conditions['shopjob.shop_id'] = $this->my->id;			
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'shopjob',
					'fields' => array('shopjob.*'),
					'conditions' => $conditions,
					'order' => '',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result = $this->fpage($tb); 
        $this->set('result',$result);			
	}	

	public function add() {
        $this->_teamRights();	
		if (!empty($this->data)) {		  
			$this->data['shop_id'] = $this->my->id;
			$this->data['add_time'] = date("Y-m-d H:i:s");										
			$safeData['shopjob'] = $this->data;			
			if (POP::exe('[@@:UM shopjob][@@:QU save]',$safeData)) {
				$this->flash('更新成功！', Router::url('shopjob/index'));		
			} else {
				$this->flash('更新失败！', Router::url('shopjob/index'));
			}					
	    }	
	}	
	
	public function edit($id) {
        $this->_teamRights();	
		if (!empty($this->data)) {					
			$this->data['shop_id'] = $this->my->id;
			$this->data['add_time'] = date("Y-m-d H:i:s");					
            if(!isset($this->data['is_show']) || empty($this->data['is_show']) ) {$this->data['is_show']=0; }		
		    $safeData['shopjob'] = $this->data;
			$query  = array();					
			$query['keyid'] = $id;				
			$query['set'] = $safeData;							
            if(POP::exe('[@@:UM shopjob][@@:QU save]',$query) ) {
				$this->flash('更新成功！', Router::url('shopjob/index'));		
			} else {
				$this->flash('更新失败！', Router::url('shopjob/index'));
			}					
	    } else {
			$query = array(
					'fields' => array('shopjob.*'),
					'conditions' => array('shopjob.id' => $id,'shopjob.shop_id' => $this->my->id),
					);	
			$data = POP::exe('[@@:UM shopjob][@@:QU find:first]',$query);			
			$data = $data['shopjob'];			
            $this->set('data',$data);				
        }	
	}

	public function del($id) {
        $this->_teamRights();	
		$query = array(
				'fields' => array('shopjob.*'),
				'conditions' => array('shopjob.id' => $id,'shopjob.shop_id' => $this->my->id),
				);	
		$news_info = POP::exe('[@@:UM shopjob][@@:QU find:first]',$query);		
        if ($news_info) {
			$query = array('keyid'=>$id);			
			POP::exe('[@@:UM shopjob][@@:QU del]',$query);			
		    $this->flash('删除成功！', Router::url('shopjob/index/'));
        }else {		
		    $this->flash('该项目不存在!', Router::url('shopjob/index/'));
        }		
	}
	
}
